package cn.wildfire.chat.app.extendsion.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Create by HEKE, 2019/9/17 9:57
 * TODO 网络数据处理
 */
public class NetworkStatusUtils {

    /**
     * 相应请求权限
     */
    private static final String PERMISSION = Manifest.permission.MODIFY_PHONE_STATE;

    /**
     * 提示打开移动数据开关接口
     *
     * @param mActivity
     * @param enable
     */
    public static void setNetDatasForMobileEnable(Activity mActivity, boolean enable, boolean forceFinish) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                setNetDatasEnable(mActivity, enable);
            } else {
                setNetDatasEnableLowSystemVersion(mActivity, enable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 5.0以下版本数据状态设置,大于最低版本8的设置(早已淘汰设备，无需考虑)
     *
     * @param context
     * @param enable
     */
    public static void setNetDatasEnableLowSystemVersion(Context context, boolean enable) {
        try {
            ConnectivityManager conman = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> conmanClass = Class.forName(conman.getClass()
                    .getName());
            Field iConnectivityManagerField = conmanClass
                    .getDeclaredField("mService");
            iConnectivityManagerField.setAccessible(true);
            Object iConnectivityManager = iConnectivityManagerField
                    .get(conman);
            Class<?> iConnectivityManagerClass = Class
                    .forName(iConnectivityManager.getClass().getName());
            Method setMobileDataEnabledMethod = iConnectivityManagerClass
                    .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
            setMobileDataEnabledMethod.setAccessible(true);
            setMobileDataEnabledMethod.invoke(iConnectivityManager, enable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 5.0及以上移动数据开启
     *
     * @param context
     * @param mobileDataEnabled
     */
    public static void setNetDatasEnable(Context context, boolean mobileDataEnabled) {
        TelephonyManager telephonyService = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method setMobileDataEnabledMethod = telephonyService.getClass()
                    .getDeclaredMethod("setDataEnabled", boolean.class);
            if (null != setMobileDataEnabledMethod) {
                setMobileDataEnabledMethod.invoke(telephonyService,
                        mobileDataEnabled);
            }
        } catch (Exception e) {
            Log.e("InstallActivity", "Errot setting"
                    + ((InvocationTargetException) e).getTargetException()
                    + telephonyService);
        }
    }

    /**
     * 返回当前数据流量允许情况
     *
     * @param context
     */
    public static boolean getMobileDataState(Context context) {
        TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
            if (null != getDataEnabled) {
                return (Boolean) getDataEnabled.invoke(telephonyService);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断wifi是否连接，但不一定有网络访问
     *
     * @param context
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetworkInfo.isConnected()) {
            return true;
        }
        return false;
    }

}
